<?php

namespace app\model\v1\repair;

use crmeb\traits\ModelTrait;
use think\facade\Db;
use think\Model;

class UserPosition extends Model
{

    use ModelTrait;

    protected $updateTime = false;
    protected $createTime = false;

    protected $hidden = [
    //    'create_time','update_time','delete_time'
    ];



    /**
     * 快修后台 - 获取员工管理列表
     * @param $param
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getUserPositionList($param = [])
    {
        $data["data"] = self::alias("a")->field('a.*,t.name as work_name')
            ->where(function ($query) use ($param){
                if(!empty($param["name"])) $query->where("a.name", 'like', '%'.$param["user_name"].'%');
                if(!empty($param["authority_id"])) $query->whereIn("a.authority_id", $param["authority_id"]);
                if(!empty($param["names"])){
                    if ($param["names"] =='kanchayuan'){
                        $query->where("a.authority_id", 4);
                    }else{
                        $query->whereIn("a.names", $param["names"]);
                        $query->where("a.authority_id", 3);
                    }
                }
                if(!empty($param["phone_number"])){
                    $query->where("a.phone_number", $param["phone_number"]);
                }
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('create_time', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
            })
            ->join('work_type t','t.id = a.names','left')
            ->page($param["page"], $param["limit"])
            ->select()
            ->toArray();

        $data["count"] = self::alias("a")->field('a.*,t.name as work_name')
            ->where(function ($query) use ($param){
                if(!empty($param["name"])) $query->where("a.name", 'like', '%'.$param["user_name"].'%');
                if(!empty($param["authority_id"])) $query->whereIn("a.authority_id", $param["authority_id"]);
                if(!empty($param["names"])){
                    if ($param["names"] =='kanchayuan'){
                        $query->where("a.authority_id", 4);
                    }else{
                        $query->whereIn("a.names", $param["names"]);
                        $query->where("a.authority_id", 3);
                    }
                }
                if(!empty($param["phone_number"])){
                    $query->where("a.phone_number", $param["phone_number"]);
                }
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('create_time', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
            })
            ->join('work_type t','t.id = a.names','left')
            ->count();

        return $data;
    }




    public function Water_delivery_worker($user_id,$name,$phone_number,$identity_img1,$identity_img2,$authority_id,$position_name,$names='',$is_baiben='',$img='',$jiguan='',$gonling='',$user_skill='',$type='',$Proportion=0,$id='')
    {
        $data['user_id']=$user_id;
        $data['name']=$name;
        $data['phone_number']=$phone_number;  //手机号
        $data['identity_img1']='';
        $data['identity_img2']='';
        $data['authority_id']=$authority_id;
        $data['position_name']=$position_name;
        $data['names']=$names;  //申请维修工种类型
        $data['is_baiben']=$is_baiben;
        $data['img']=$img;
        $data['jiguan']=$jiguan;
        $data['gonling']=$gonling;
        $data['user_skill']=$user_skill;
        $data['type']=$type;
        $data['proportion']=$Proportion;
        if (empty($id)){
            $res = Db::name('user_position')->insert($data);
        }else{
            $res = Db::name('user_position')->where('id',$id)->update($data);
        }
        if ($res){
            return  true;
        }
        return  false;
    }





}